package com.headwind.error;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/12
 * @description : 统一业务异常处理类
 */
@ControllerAdvice(basePackages = {"com.headwind",})
public class GlobalDefaultExceptionHandler {

    /**
     * 如果返回的为json数据或其它对象，添加该注解
     */
    @ExceptionHandler({BusinessException.class})
    @ResponseBody
    public ErrorInfo defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setMessage(e.getMessage());
        errorInfo.setUrl(req.getRequestURI());
        errorInfo.setCode(ErrorInfo.SUCCESS);
        return errorInfo;
    }

}
